/*
 * Use for load partial page content
 * @param strUrl : Url request to servlet
 * @param strContent : content to be placed normally use class name
 */
function ContentAjaxRequest(strUrl, strContent, isRemoveDialog) {
    /**
     * Default Param
     */
    if(isRemoveDialog === undefined)
    {
        isRemoveDialog = true;
    }
    /**
     * End Default Param
     */

    /**
     * Clear Dialog When Change Page
     * !!Rule : Dialog must have class "medhub_dialog"
     */
     $(".ui-dialog").dialog("destroy");
    if(isRemoveDialog)
    {
       
        $(".medhub_dialog").remove();
    }
    /**
     * End Clear Dialog When Change Page
     */
    
    $(strContent).html('<div class="content-box" style="text-align: center;"><img src="images/loading.gif" /></div>');
    $(strContent).load(strUrl, "",
        function (responseText, textStatus, XMLHttpRequest) {
            if (textStatus == "success") {
            // all good!
            }
            if (textStatus == "error") {
                // oh noes!
                alert(responseText);
            }
        }
        );
}

function ShowLogin() {
    $("div #panel").slideDown("slow");
    $("#toppanel #open").hide();
    $("#toppanel #close").show();
// @TODO : Clear user Log out
}

function populateSelect(el, items ,strDefault, idSelected) {
    if (el==null)return false;
    var index=0;
    el.options.length = 0;
    if (items.length > 0)
        el.options[0] = new Option(strDefault, '');
    $.each(items, function () {
        el.options[el.options.length] = new Option(this.name, this.value);
        if (this.value==idSelected) // use id value for option selected
            index=el.options.length-1;
    });
    if (idSelected==""){
        el.options[0].selected = true; // set strDefault for selected
    }else{
        el.options[index].selected = true; // set index from value for selected
    }
}

/*
* Bind Json Data To Html Element By ID
* @param responseText : Json Data
*/
function BindJsonToHtml(responseText) {
    $.each(responseText, function(key, value) {               // Iterate over the JSON object.
        if (key == 'attr') {
            $.each(value, function() {
                var id;
                $.each(this, function(formKey, formValue) {
                    if (formKey == 'id') {
                        id = formValue;
                    } else if (formKey == 'mapAttr') {
                        $(id).attr(formValue); //User .attr(map) To map all attribute then "map" = {key:value, key:value,...}
                    }
                });
            });
        } else if (key == 'removeAttr') {
            $.each(value, function(id, val) {
                $(id).removeAttr(val);
            });
        } else if (key == 'html') {
            $.each(value, function(id, val) {
                $(id).html(val);
            });
        } else if (key == 'text') {
            $.each(value, function(id, val) {
                $(id).text(val);
            });
        } else if (key == 'val') {
            $.each(value, function(id, val) {
                $(id).val(val);
            });
        } else if (key == 'addClass') {
            $.each(value, function(id, val) {
                $(id).addClass(val);
            });
        } else if (key == 'removeClass') {
            $.each(value, function(id, val) {
                $(id).removeClass(val);
            });
        } else if (key == 'toggleClass') {
            $.each(value, function(id, val) {
                $(id).toggleClass(val);
            });
        } else if (key == 'showHide') {
            $.each(value, function(id, val) {
                if (val == "1") {
                    $(id).show();
                } else if (val == "0") {
                    $(id).hide();
                }

            });
        } else if (key == 'append') {
            $.each(value, function(id, val) {
                $(id).append(val);
            });
        } else if (key == 'appendTo') {
            $.each(value, function(id, val) {
                $(id).appendTo(val);
            });
        } else if (key == 'replaceWith') {
            $.each(value, function(id, val) {
                $(id).replaceWith(val);
            });
        } else if (key == 'replaceAll') {
            $.each(value, function(id, val) {
                $(id).replaceAll(val);
            });
        } else if (key == 'remove') {
            $.each(value, function(id, val) {
                $(id).remove(val);
            });
        } else if (key == 'detach') {
            $.each(value, function(id, val) {
                $(id).detach(val);
            });
        } else if (key == 'empty') {
            $.each(value, function(id, val) {
                $(id).empty(val);
            });
        } else if (key == 'clone') {
            $.each(value, function(id, val) {
                $(id).clone(val);
            });
        } else if (key == 'dialog') {
            $.each(value, function() {
                var id = "";
                var uiState = "";
                var uiIcon = "";
                var dialogTitle = "";
                $.each(this, function(msgKey, msgValue) {
                    if (msgKey == 'id') {
                        id = msgValue;
                        dialogTitle = "#ui-dialog-title-" + id;
                    } else if (msgKey == 'mapMsg') {
                        $.each(msgValue, function(msgCode, msgVar) {
                            if (msgCode == 'uiState') {
                                uiState = 'ui-state-' + msgVar;

                                //Set uiIcon By State : เราจะกำหนด icon ตาม state เพื่อจะได้ใช้ icon เดียวกัน
                                if (msgVar == 'highlight') { //For Complete
                                    uiIcon = '<span class="ui-icon ui-icon-circle-check" style="float:left;"></span>';
                                } else if (msgVar == 'error') { //For Error
                                    uiIcon = '<span class="ui-icon ui-icon-circle-close" style="float:left;"></span>';
                                } else if (msgVar == 'focus') { //For Warning
                                    uiIcon = '<span class="ui-icon ui-icon-alert" style="float:left;"></span>';
                                } else if (msgVar == 'default') { //For Confirm
                                    uiIcon = '<span class="ui-icon ui-icon-help" style="float:left;"></span>';
                                }
                            } else if (msgCode == 'uiIcon') { //ถ้าอยากเปลี่ยน icon เอง
                                uiIcon = '<span class="ui-icon ui-icon-' + msgVar + '" style="float:left;"></span>';
                            } else if (msgCode == 'msgTitle') {
                                //ใส่ title ก่อน
                                $(dialogTitle).text(constantMsg[msgVar]); //constantMsg[msgVar] จะทำให้เปลี่ยนภาษาได้
                                //ใส่ icon และเปลี่ยน state
                                $(dialogTitle).prepend(uiIcon).parent().addClass(uiState)
                            } else if (msgCode == 'msgContent') {
                                $(id).text(constantMsg[msgVar])
                            } else if (msgCode == 'isOpen') {
                                if (msgVar == '1') {
                                    $(id).dialog( "open" )
                                }
                            }

                            $(id).attr(msgValue);
                        });
                        
                    }
                });
            });
        }
    });
}


